// 移动端适配工具函数

// 检测是否为移动设备
export function isMobile() {
  return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ||
         window.innerWidth <= 768
}

// 检测是否为触摸设备
export function isTouchDevice() {
  return 'ontouchstart' in window || navigator.maxTouchPoints > 0
}

// 获取设备类型
export function getDeviceType() {
  const width = window.innerWidth
  if (width <= 480) return 'mobile-small'
  if (width <= 768) return 'mobile'
  if (width <= 1024) return 'tablet'
  return 'desktop'
}

// 检测屏幕方向
export function getOrientation() {
  return window.innerHeight > window.innerWidth ? 'portrait' : 'landscape'
}

// 监听屏幕尺寸变化
export function onScreenResize(callback) {
  let timeout
  const handleResize = () => {
    clearTimeout(timeout)
    timeout = setTimeout(() => {
      callback({
        width: window.innerWidth,
        height: window.innerHeight,
        deviceType: getDeviceType(),
        orientation: getOrientation(),
        isMobile: isMobile(),
        isTouch: isTouchDevice()
      })
    }, 100)
  }
  
  window.addEventListener('resize', handleResize)
  
  // 返回清理函数
  return () => {
    window.removeEventListener('resize', handleResize)
    clearTimeout(timeout)
  }
}

// 移动端优化的表格配置
export function getMobileTableConfig() {
  return {
    stripe: true,
    border: false,
    size: 'small',
    showOverflowTooltip: true
  }
}

// 移动端优化的分页配置
export function getMobilePaginationConfig() {
  return {
    small: true,
    background: true,
    layout: 'total, sizes, prev, pager, next, jumper'
  }
}

// 移动端优化的对话框配置
export function getMobileDialogConfig() {
  return {
    width: isMobile() ? '95%' : '700px',
    top: isMobile() ? '5vh' : '15vh',
    closeOnClickModal: false,
    closeOnPressEscape: true
  }
}

// 移动端优化的表单配置
export function getMobileFormConfig() {
  return {
    labelWidth: isMobile() ? '80px' : '120px',
    labelPosition: isMobile() ? 'top' : 'right',
    size: isMobile() ? 'small' : 'default'
  }
}

// 防抖函数
export function debounce(func, wait) {
  let timeout
  return function executedFunction(...args) {
    const later = () => {
      clearTimeout(timeout)
      func(...args)
    }
    clearTimeout(timeout)
    timeout = setTimeout(later, wait)
  }
}

// 节流函数
export function throttle(func, limit) {
  let inThrottle
  return function() {
    const args = arguments
    const context = this
    if (!inThrottle) {
      func.apply(context, args)
      inThrottle = true
      setTimeout(() => inThrottle = false, limit)
    }
  }
}

// 移动端触摸优化
export function optimizeTouchTarget(element, minSize = 44) {
  if (isTouchDevice()) {
    element.style.minHeight = `${minSize}px`
    element.style.minWidth = `${minSize}px`
  }
}

// 移动端滚动优化
export function enableSmoothScroll() {
  if (isMobile()) {
    document.documentElement.style.scrollBehavior = 'smooth'
  }
}

// 移动端字体大小调整
export function adjustFontSize() {
  const deviceType = getDeviceType()
  const fontSizeMap = {
    'mobile-small': '13px',
    'mobile': '14px',
    'tablet': '15px',
    'desktop': '16px'
  }
  
  document.documentElement.style.fontSize = fontSizeMap[deviceType] || '16px'
}

// 初始化移动端适配
export function initMobileAdaptation() {
  adjustFontSize()
  enableSmoothScroll()
  
  // 监听屏幕变化
  onScreenResize(() => {
    adjustFontSize()
  })
} 